package com.ylbh.modules.newinfo.controller;

import com.ylbh.common.annotation.SysLog;
import com.ylbh.common.pojo.newinfo.dto.AddNewInfoDTO;
import com.ylbh.common.pojo.newinfo.dto.NewInfoDetailsDTO;
import com.ylbh.common.pojo.newinfo.dto.NewInfoListDTO;
import com.ylbh.common.pojo.newinfo.vo.NewInfoDetailsVO;
import com.ylbh.common.pojo.newinfo.vo.NewsClassVO;
import com.ylbh.common.utils.CommonResult;
import com.ylbh.common.utils.PageUtils;
import com.ylbh.common.utils.Ref;
import com.ylbh.modules.newinfo.service.ShoppingNewsInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.List;

/**
 *
 * 新闻管理
 * @author czx
 * @date 2019-04-11 10:31:38
 */
@RestController
@RequestMapping("/newInfo")
public class ShoppingNewsInfoController {

    @Autowired
    private ShoppingNewsInfoService shoppingNewsInfoService;

    @ApiOperation("新闻内容列表")
    @RequestMapping("queryNewInfoList")
    @RequiresPermissions("newInfo:queryNewInfoList")
    public CommonResult<PageUtils> queryNewInfoList(NewInfoListDTO newInfoListDTO) {
        PageUtils list = shoppingNewsInfoService.queryNewsInfoList(newInfoListDTO);
        return Ref.ok(list);
    }

    @ApiOperation("新闻内容详情")
    @RequestMapping("getNewInfoDetailsByNewsId/{newsId}")
    @RequiresPermissions("newInfo:getNewInfoDetailsByNewsId")
    public CommonResult<NewInfoDetailsVO> getNewInfoDetailsByNewsId(@PathVariable(value = "newsId")String newsId) {
        NewInfoDetailsVO vo = shoppingNewsInfoService.getNewInfoDetailsByNewsId(newsId);
        return Ref.ok(vo);
    }

    @SysLog("修改新闻内容")
    @ApiOperation("修改新闻内容")
    @RequestMapping("modifyNewInfoDetailsByNewsId")
    @RequiresPermissions("newInfo:modifyNewInfoDetailsByNewsId")
    public CommonResult<Void> modifyNewInfoDetailsByNewsId(@RequestBody NewInfoDetailsDTO newInfoDetailsDTO) {
        shoppingNewsInfoService.modifyNewInfoDetailsByNewsId(newInfoDetailsDTO);
        return Ref.ok();
    }

    @SysLog("删除新闻")
    @ApiOperation("删除新闻")
    @RequestMapping("removeNewInfoByNewsIds")
    @RequiresPermissions("newInfo:removeNewInfoByNewsIds")
    public CommonResult<Void> removeNewInfoByNewsIds(@RequestBody String[] newsIds) {
        shoppingNewsInfoService.removeNewInfoByNewsIds(Arrays.asList(newsIds));
        return Ref.ok();
    }

    @SysLog("新增新闻内容")
    @ApiOperation("新增新闻内容")
    @RequestMapping("addNewsInfo")
    @RequiresPermissions("newInfo:addNewsInfo")
    public CommonResult<Void> addNewsInfo(@RequestBody AddNewInfoDTO addNewInfoDTO) {
        shoppingNewsInfoService.addNewsInfo(addNewInfoDTO);
        return Ref.ok();
    }

    @ApiOperation("上传新闻图片")
    @RequestMapping("uploadNewsInfo")
    public CommonResult<String> uploadNewsInfo(MultipartFile file){
        String url = shoppingNewsInfoService.uploadNewsInfo(file);
        return Ref.ok(url);
    }

    @ApiOperation("上传新闻视频")
    @RequestMapping("uploadNewsInfoVideo")
    public CommonResult<String> uploadNewsInfoVideo(MultipartFile file, HttpServletRequest request){
        String url = shoppingNewsInfoService.uploadNewsInfoVideo(file, request);
        return Ref.ok(url);
    }

    @ApiOperation("获取全部分类")
    @RequestMapping("queryClass")
    public CommonResult<List<NewsClassVO>> queryClass() {
        List<NewsClassVO> aClass = shoppingNewsInfoService.queryClass();
        return Ref.ok(aClass);
    }
}
